<template>
  <div @keyup.enter="getData()">
    <el-form class="query-form" :inline="true" :model="params">
      <el-form-item label="名称查询">
        <el-input v-model="params.customer_name" placeholder="主体名称查询" clearable>
          <i class="el-icon-search el-input__icon" slot="suffix" @click="getData()"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="所属区域">
        <select-address
          v-model="params.address_code"
          :auto-top="true"
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item label="组织形式">
        <select-farm-org v-model="params['org_class.eq']" @change="getData()"></select-farm-org>
      </el-form-item>
      <el-form-item label="企业类型">
        <select-farm-type v-model="params['farm_type.eq']" @change="getData()"></select-farm-type>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dialogImportVisible = true">
          <i class="fa fa-fw fa-upload"></i>
          导入项目申请信息
        </el-button>
      </el-form-item>
    </el-form>

    <div v-loading="loading">
      <el-table border :data="tableData">
        <el-table-column align="center" label="贷款主体证件号">
          <template slot-scope="scope">
            {{ scope.row.lender_idcard }}
          </template>
        </el-table-column>
        <el-table-column label="经营主体名称" min-width="160">
          <template slot-scope="scope">
            <a :href="farmUrl" target="_blank">
              {{ scope.row.customer_name }}
            </a>
          </template>
        </el-table-column>
        <el-table-column label="经营地址" min-width="170">
          <template slot-scope="scope">
            <el-tooltip effect="dark" :content="scope.row.address" placement="bottom-start">
              <span>{{ scope.row.address | substr }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="法人" prop="corp_name" min-width="100"></el-table-column>
        <el-table-column label="电话" prop="mobile_no" min-width="110"></el-table-column>
        <el-table-column label="企业类型">
          <template slot-scope="scope">
            {{ enumFarmType[scope.row.farm_type] }}
          </template>
        </el-table-column>

        <el-table-column label="组织形式">
          <template slot-scope="scope">
            {{ enumOrgClass[scope.row.org_class] }}
          </template>
        </el-table-column>
        <el-table-column label="经营范围" min-width="100">
          <template slot-scope="scope">
            <span class="text-truncate" :title="scope.row.bussiness_scope">
              {{ scope.row.bussiness_scope }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="apply_funds" label="申请金额/(万元)"></el-table-column>
        <el-table-column label="用途" min-width="160">
          <template slot-scope="scope">
            <el-tooltip effect="dark" :content="scope.row.funds_use_dir" placement="bottom-start">
              <span>{{ scope.row.funds_use_dir | substr }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="客户来源">
          <template slot-scope="scope">
            {{ enumCustomerSource[scope.row.customer_source] }}
          </template>
        </el-table-column>
        <el-table-column label="录入时间" min-width="90">
          <template slot-scope="scope">
            {{ scope.row.create_time | timeDay }}
          </template>
        </el-table-column>
        <el-table-column prop="submit_person_name" label="提交人" min-width="160">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              :content="scope.row.submit_person_name"
              placement="bottom-start"
            >
              <span>{{ scope.row.submit_person_name | substr }}</span>
            </el-tooltip>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              v-if="
                (scope.row.project_status === 0 || scope.row.project_status === 2) &&
                userId &&
                scope.row.submit_person === userId
              "
              content="修改信息"
              placement="bottom"
            >
              <el-button type="text" class="py-0 px-1" @click="infoEdit(scope.row)">
                <i class="fa fa-edit"></i>
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <div class="query-pagination">
        <el-pagination
          @current-change="getData"
          :current-page.sync="params.index"
          :page-size="params.size"
          layout="total, prev, pager, next, jumper"
          :total="totalCount"
        ></el-pagination>
      </div>
    </div>

    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>

    <new-cmpt
      :visible.sync="newVisible"
      :customer-related-id="customerRelatedId"
      @success="getData(params.index)"
    ></new-cmpt>

    <el-dialog
      :visible.sync="reportVisible"
      :before-close="CloseReport"
      :close-on-click-modal="false"
      width="80%"
    >
      <credit-file :related-id="attachRelatedId"></credit-file>
    </el-dialog>

    <el-dialog
      :visible.sync="reportView"
      :before-close="CLoseRepotView"
      :close-on-click-modal="false"
      width="80%"
    >
      <credit-file :view-mode="true" :related-id="attachRelatedId"></credit-file>
    </el-dialog>

    <import-excel
      :visible.sync="dialogImportVisible"
      title="导入项目申请信息"
      :post-api="postUrl"
      excel-template="/static/files/项目申请导入模板.xlsx"
      @success="getData(params.index)"
    ></import-excel>
  </div>
</template>
<script>
import CreditFile from '@/credit/components/creditFile/Index'
import SelectAddress from '@/components/select/SelectAddress'
import SelectFarmOrg from '@/components/select/SelectFarmOrg'
import SelectFarmType from '@/credit/components/select/SelectFarmType'
import AttachCmpt from '../program/Attach'
import NewCmpt from '../program/New'
import ImportExcel from '@/components/ImportExcel'
import {
  enumFarmType,
  enumCustomerSource,
  enumApprovalStatus,
  enumOrgClass,
  enumCreditType,
} from '@/credit/plugins/enum'
import { BASE_URL_H5 } from '@/common/plugins/constant'
import { guid } from '@vimi/utils-tool'

export default {
  components: {
    CreditFile,
    SelectAddress,
    SelectFarmOrg,
    SelectFarmType,
    AttachCmpt,
    NewCmpt,
    ImportExcel,
  },
  props: {},
  data() {
    return {
      postUrl: '',
      dialogImportVisible: false,
      enumFarmType,
      userId: '',
      // job: '',
      enumCustomerSource,
      enumApprovalStatus,
      enumCreditType,
      enumOrgClass,
      enumRecommend: {
        1: '是',
        2: '否(农但)',
        3: '否(其他)',
      },
      params: {
        index: 1,
        size: 10,
        address_code: '',
        customer_name: '',
        'submit_type.eq': '',
        'credit_type.eq': '',
        'org_class.eq': '',
        'farm_type.eq': '',
        'service_id.eq': '',
      },
      loading: false,
      tableName: 'customer_related',
      tableData: [],
      totalCount: 0,
      // 附件
      attachRelatedId: '',
      attachVisible: false,
      // 新增
      newVisible: false,
      file: 0,
      reportVisible: false,
      reportView: false,
      customerRelatedId: '',
    }
  },
  computed: {
    org() {
      return this.$store.getters.org
    },
    job() {
      return this.$store.getters.user.job
    },
    superJob() {
      return [
        'org_credit_super',
        'org_c_manage',
        'org_r_manage',
        'org_p_manage',
        'org_c_general',
      ].includes(this.job)
    },
  },
  watch: {},
  created() {
    this.params['service_id.eq'] = this.org.clientId
    this.userId = this.$store.getters.user.userId
    // this.job = this.$store.getters.user.job

    this.postUrl =
      '/api/credit/import/?importKey=customer_related_template&where=' +
      encodeURIComponent(
        '"customer_related.service_id".eq(' + this.$store.getters.org.clientId + ')'
      ) +
      encodeURIComponent('&"customer_related.customer_related_id".eq(' + guid() + ')')

    this.getData()
  },
  mounted() {},
  methods: {
    async getData(index = 1) {
      this.params.index = index
      this.newVisible = false
      this.loading = true
      let [res, err] = await this.$utils.promise(
        this.$api.getData('credit', this.tableName, this.params, { create_time: 'desc' })
      )
      this.loading = false
      if (err) {
        return this.$message.error('获取预审库失败，请稍后重试！')
      }

      const data = res.data.data || []
      this.tableData = data.map(d => {
        d.farmUrl = `${BASE_URL_H5}?farmId=${d.customer_id}`
        d.approvalStatus = enumApprovalStatus[d.is_approval] || {}
        d.project_status = +d.project_status
        return d
      })
      this.totalCount = res.data.totalCount
    },

    async handleCommand(c, id, cs) {
      if (c === cs) {
        return
      }
      let [res] = await this.$utils.promise(
        this.$api.updateData('credit', 'customer_related', id, {
          is_approval: c,
          approved_date: this.$utils.dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss'),
        })
      )

      if (res && res.data > 0) {
        this.getData()
        return this.$message.success('修改成功！')
      }
      this.$message.error('修改失败，请重试！')
    },

    onAttach(row) {
      this.attachRelatedId = row.customer_related_id
      this.attachVisible = true
    },

    onNew() {
      this.customerRelatedId = ''
      this.newVisible = true
    },
    CloseReport() {
      this.reportVisible = false
    },
    CLoseRepotView() {
      this.reportView = false
    },
    goInfo(data, view) {
      sessionStorage.setItem('customer_related_id', data.customer_related_id)
      sessionStorage.setItem('customer_related_view', JSON.stringify(data))
      sessionStorage.setItem('credit_task_router', 'prelim')
      this.file++
      this.attachRelatedId = data.customer_related_id
      if (view === true) {
        this.reportView = true
      } else {
        this.reportVisible = true
      }
    },
    infoEdit(data) {
      this.customerRelatedId = data.customer_related_id
      this.newVisible = true
    },
  },
}
</script>
<style scoped></style>
